<?php
/**
 * Created by PhpStorm.
 * User: 35083806
 * Date: 2020/1/15
 * Time: 15:40
 */

include_once(dirname(realpath(APP_PATH)) . "/vendor/aliyun-oss-php-sdk-master/samples/Common.php");

use OSS\OssClient;

/**
 * 上传文件到oss服务器
 * @param $localPath eg :"E:\\xampp\htdocs\\flower\sleep\public\static\images\logo1.png";
 * @param $ossPath ，eg  'images/logo1.png'
 * @return null
 * @throws \OSS\Core\OssException
 */
function ossUpload($localPath, $ossPath)
{
    $bucketName = Common::getBucketName();
    $ossClient = Common::getOssClient();
    if (is_null($ossClient)) {
        exit(1);
    }
    return $ossClient->uploadFile($bucketName, $ossPath, $localPath);
}

/**
 * 保存一个对象
 * @param $filename eg：data/aa.file
 * @param $content eg:{'a':'nihao'}
 * @return null
 */
function putObject($filename, $content)
{
    $bucket = Common::getBucketName();
    $ossClient = Common::getOssClient();
    if (is_null($ossClient)) exit(1);
    return $ossClient->putObject($bucket, $filename, $content);
}

/**
 * 获取一个对象内容
 * @param $filename eg：data/aa.file
 * @return string
 */
function getObject($filename)
{
    $bucket = Common::getBucketName();
    $ossClient = Common::getOssClient();
    if (is_null($ossClient)) exit(1);
    return $content = $ossClient->getObject($bucket, $filename);
}



/**
 * Set bucket lifecycle configuration
 *
 * @param OssClient $ossClient OssClient instance
 * @param string $bucket bucket name
 * @return null
 */
function putBucketLifecycle($ossClient, $bucket)
{
    $bucketName = Common::getBucketName();
    $ossClient = Common::getOssClient();
    if (is_null($ossClient)) {
        exit(1);
    }
    $lifecycleConfig = new LifecycleConfig();
    $actions = array();
    $actions[] = new LifecycleAction(OssClient::OSS_LIFECYCLE_EXPIRATION, OssClient::OSS_LIFECYCLE_TIMING_DAYS, 3);
    $lifecycleRule = new LifecycleRule("delete obsoleted files", "obsoleted/", "Enabled", $actions);
    $lifecycleConfig->addRule($lifecycleRule);
    $actions = array();
    $actions[] = new LifecycleAction(OssClient::OSS_LIFECYCLE_EXPIRATION, OssClient::OSS_LIFECYCLE_TIMING_DATE, '2022-10-12T00:00:00.000Z');
    $lifecycleRule = new LifecycleRule("delete temporary files", "temporary/", "Enabled", $actions);
    $lifecycleConfig->addRule($lifecycleRule);
    try {
        $ossClient->putBucketLifecycle($bucket, $lifecycleConfig);
    } catch (OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    print(__FUNCTION__ . ": OK" . "\n");
}

/**
 * Get bucket lifecycle configuration
 *
 * @param OssClient $ossClient OssClient instance
 * @param string $bucket bucket name
 * @return null
 */
function getBucketLifecycle($ossClient, $bucket)
{
    $lifecycleConfig = null;
    try {
        $lifecycleConfig = $ossClient->getBucketLifecycle($bucket);
    } catch (OssException $e) {
        printf(__FUNCTION__ . ": FAILED\n");
        printf($e->getMessage() . "\n");
        return;
    }
    print(__FUNCTION__ . ": OK" . "\n");
    print($lifecycleConfig->serializeToXml() . "\n");
}